﻿// <copyright file="DoubleTag.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xin-ping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// A <see cref="Tag"/> with a double value.
    /// </summary>
    public class DoubleTag : Tag<double>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DoubleTag"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        public DoubleTag(string name)
            : base(name)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DoubleTag"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        /// <param name="value">The value of this tag.</param>
        public DoubleTag(string name, double value)
            : base(name, value)
        {
        }

        /// <summary>
        /// Converts the value of this instance to text.
        /// </summary>
        /// <returns>Converted text representing the value.</returns>
        public override string ValueToText()
        {
            return this.Value.ToString();
        }

        /// <summary>
        /// Parses the value of this instance from text.
        /// </summary>
        /// <param name="text">The text to be parsed to generate value.</param>
        public override void ValueFromText(string text)
        {
            try
            {
                this.Value = double.Parse(text);
            }
            catch
            {
                throw new ParseException("Can not parse a double from " + text);
            }
        }

        /// <summary>
        /// Converts the value of this tag to a float.
        /// </summary>
        /// <returns>The converted float value.</returns>
        public float ValueToFloat()
        {
            if (this.Value > float.MaxValue)
            {
                return float.MaxValue;
            }
            else if (this.Value < float.MinValue)
            {
                return float.MinValue;
            }
            else
            {
                return (float)this.Value;
            }
        }
    }
}
